package cn.wolfcode.p2p.base.controller;

import cn.wolfcode.p2p.anno.NeedLogin;
import cn.wolfcode.p2p.base.domain.RealAuth;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.service.IRealAuthSevice;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.util.JSONResultUtil;
import cn.wolfcode.p2p.util.UploadUtil;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * 前台真实认证
 */
@Controller
public class RealAuthController {
    @Autowired
    private IRealAuthSevice realAuthSevice;
    @Autowired
    private IUserInfoService userInfoService;

    @Value("${basePath}")
    public String path;
    @Value("${upload}")
    public String upload;

    @NeedLogin
    @RequestMapping("realAuth")
    public String realAuth(Model model){
        UserInfo userInfo = userInfoService.getUserInfo(UserContext.getLoginInfo().getId());
        //如果位状态有相应值,realAuthId为1则跳转到成功页面
        if (userInfo.hasRealAuth()) {
            model.addAttribute("realAuth",realAuthSevice.getRealAuth(userInfo.getRealAuthId()));
            return "realAuth_result";
        }
        //realAuthId为1则跳转到成功页面,并共享auditing
        if(userInfo.getRealAuthId() != null){
            model.addAttribute("auditing",true);
            return "realAuth_result";
        }
        //如果realAuthId为空则跳转到申请页面
        return "/realAuth";
    }

    @NeedLogin
    @RequestMapping("realAuth_save")
    @ResponseBody
    public Object save(RealAuth realAuth){
        realAuthSevice.apply(realAuth);
        return JSONResultUtil.jsonResult();
    }

    @NeedLogin
    @RequestMapping("realAuth_image")
    @ResponseBody
    public Object upload(MultipartFile fileName){
        String imageUrl = UploadUtil.upload(fileName, path + upload);
        return upload + imageUrl;
    }

}

